import {
	buildPageUrl,
	formatDate,
	encodeRedirectUrl,
	decodeRedirectUrl
} from '@/common/util.js'
import {
	silentReload,
	getRedirectUrl
} from '@/service/request/main.js'
import {
	addCart
} from '@/service/api/order.js'

export default {
	data() {
		return {
			foodData: null
		}
	},
	onLoad(options) {
		console.log('onload mixin common', options)
		// 自动登录
		if (!this.$store.getters.accessToken) {
			if (!this.$store.getters.autoLogin) {
				console.info('common onload', 'silentReload')
				silentReload()
				this.$store.commit('setAutoLogin', 1)
			}
		}
	},
	computed: {
		onlyHome() {
			return getCurrentPages().length === 1
		},
		timeFormat() {
			return (timestamp, format) => {
				if (!timestamp) {
					return ''
				}

				return formatDate(timestamp, format || 'yyyy-MM-dd hh:mm:ss')
			}
		},
		isLogin() {
			return this.$store.getters.isLogin
		},
		showCart() {
			return this.$store.getters.cartCount > 0
		}
	},
	methods: {
		onCopy(text) {
			uni.setClipboardData({
				data: text + ''
			}).then(([err, res]) => {
				console.log('uni.setClipboardData', err, res)
				if (!err) {
					uni.showToast({
						title: '复制成功',
						icon: 'none',
					})
				}
			})
		},
		checkLogin() {
			if (!this.isLogin) {
				uni.navigateTo({
					url: '/pages/user/login?from=' + encodeRedirectUrl(getRedirectUrl())
				})
				return false
			}

			return true
		},
		redirect(type, data) {
			console.log('redirect', type, data)
			if (type === 'link') {
				uni.navigateTo({
					url: '/pages/home/webview?url=' + encodeURIComponent(data.url)
				})
			} else if (type === 'page') {
				let page = data.page
				if (page.indexOf('/pages/food/index') !== -1 ||
					page.indexOf('/pages/order/list') !== -1 ||
					page.indexOf('/pages/user/mine') !== -1) {
					uni.switchTab({
						url: page
					})
				} else {
					uni.navigateTo({
						url: page
					})
				}
			} else if (type === 'source') {
				if (data.type === 'page') {
					uni.navigateTo({
						url: '/pages/content/page_detail?id=' + data.model_id
					})
				} else if (data.type === 'food') {
					uni.navigateTo({
						url: '/pages/food/detail?id=' + data.model_id
					})
				}
			}

			return false
		},
		onRedirect(type, muteLogin) {
			console.log('onRedirect', type, muteLogin)
			if (!muteLogin) {
				if (!this.checkLogin()) {
					return
				}
			}
			var pages = {
				'user-login': '/pages/user/login',
				'mine-fund': '/pages/user/fund',
				'mine-like': '/pages/user/like',
				'food-index': '/pages/food/index',
				'order-list': '/pages/order/list',
				'user-info': '/pages/user/info',
				'question-list': '/pages/content/question',
				'feedback': '/pages/user/feedback',
				'setting': '/pages/user/setting'
			}
			var page = pages[type] || ''
			if (page) {
				if (type === 'food-index' || type === 'order-list') {
					uni.switchTab({
						url: page
					})
				} else {
					uni.navigateTo({
						url: page
					})
				}
			}
		},
		onShowFood(e) {
			console.log('onShowFood', e)
			this.foodData = e
			this.$refs.quickCart.close()
			this.$refs.foodView.show()
		},
		onShowCart() {
			this.$refs.quickCart.show()
		},
		onAddCart(e) {
			console.log('onAddCart', e)
			if (e && e.goods_id) {
				addCart(e.goods_id, e.goods_name, e.goods_cover, e.goods_price, e.goods_data, e.goods_count)
					.then(([err, res]) => {
						console.log('addCart', err, res)
						if (!err) {
							this.$store.commit('addCartItem', res)
							this.onShowCart()
						}
					})
			} else {
				this.onShowCart()
			}
		}
	}
}